//
// Created by Ctzzz on 2024/4/26.
//

#include "TriangleSample.h"
#include "GLUtils.h"

TriangleSample::TriangleSample()
{

}

TriangleSample::~TriangleSample()
{
    // 如果资源不为空，释放资源
    if (m_ProgramObj)
    {
        glDeleteProgram(m_ProgramObj);
    }
}

void TriangleSample::Init()
{
    // 顶点坐标, 三角形
    char vShaderStr[] =
            "#version 300 es                          \n"
            "layout(location = 0) in vec4 vPosition;  \n"
            "void main()                              \n"
            "{                                        \n"
            "   gl_Position = vPosition;              \n"
            "}                                        \n";
    // 片元颜色, 红色
    char fShaderStr[] =
            "#version 300 es                              \n"
            "precision mediump float;                     \n"
            "out vec4 fragColor;                          \n"
            "void main()                                  \n"
            "{                                            \n"
            "   fragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );  \n"
            "}                                            \n";

    m_ProgramObj = GLUtils::CreateProgram(vShaderStr, fShaderStr, m_VertexShader, m_FragmentShader);

}

void TriangleSample::Draw()
{
    LOGCATE("TriangleSample::Draw");
    GLfloat vVertices[] = {
            0.0f,  0.5f, 0.0f,
            -0.5f, -0.5f, 0.0f,
            0.5f, -0.5f, 0.0f,
    };

    if(m_ProgramObj == 0)
        return;

//    // 设置视口
//    glViewport(0, 0, screenW, screenH);
//    // 清屏
//    glClear(GL_COLOR_BUFFER_BIT);
    // 使用程序
    glUseProgram(m_ProgramObj);
    // 设置顶点坐标
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
    glEnableVertexAttribArray(0);
    // 绘制三角形
    glDrawArrays(GL_TRIANGLES, 0, 3);
}

void TriangleSample::LoadImage(NativeImage *pImage)
{
    //null implement

}